home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ GX 1.1.2 / Programming Stuff / QuickDraw™ GX Libraries / Printing Libraries / PicturesAndPICTLibrary.c < prev   
Encoding:
C/C++ Source or Header  |  1995-04-10  |  5.3 KB  |  187 lines  |  [TEXT/MPS ]

  1. /*
  2.     Libraries for dealing with pictures within PICTs
  3.     copyright © 1992 Apple Computer, Inc.
  4.     All rights reserved.
  5.     
  6.     
  7.     Routines in this library:
  8.         PictureToPICT         - creates a PICT with an embedded picture shape
  9. */
  10. #include <Types.h>
  11. #include <Quickdraw.h>
  12. #include <ToolUtils.h>
  13. #include <Memory.h>
  14.  
  15. #include "graphics routines.h"
  16. #include "graphics toolbox.h"
  17. #include "math routines.h"
  18. #include "storage library.h"
  19. #include "offscreen library.h"
  20. #include "qd library.h"
  21.  
  22. //-----------------------------------------------------------------------------
  23. // INTERNAL TYPEDEFS AND DEFINES
  24. //-----------------------------------------------------------------------------
  25.  
  26. // IDs for the PicComments
  27. #define shapeSignature    'qdgx'
  28. #define shapeBegin        500
  29. #define shapeEnd        501
  30.  
  31. // typedefs for the new PicComments
  32. typedef struct 
  33.     {
  34.     OSType        signature;        // always == shapeSignature
  35.     short        kind;            // always == shapeBegin
  36.     Rect        bounds;            // bounds of shape @ 72 dpi
  37.     char        data[1];        // flattened shape data, total size of record determines
  38.                                 // size
  39.     } ShapeBeginRecord, *ShapeBeginPtr, **ShapeBeginHdl;
  40.     
  41. typedef struct
  42.     {
  43.     OSType        signature;        // always == shapeSignature
  44.     short        kind;            // always == shapeEnd
  45.     } ShapeEndRecord, *ShapeEndPtr, **ShapeEndHdl;
  46.  
  47. //-----------------------------------------------------------------------------
  48.  
  49. PicHandle    PictureToPICT(gxShape theShape, Boolean simpleProxy)
  50. /*
  51.     This library routine turns a QuickDraw GX™ shape into a QuickDraw PICT.
  52.     It does this using three steps:
  53.         1)  it emits a QuickDraw PicComment, and places the flattened GX data into
  54.             the comment.
  55.         2)    it converts the GX picture into a QuickDraw proxy.  It uses a rectangle
  56.             with an 'X' through it if simpleProxy is true.  Otherwise, it uses a 1 bit
  57.             bitmap rendition of the shape.
  58.         3)    it emits a QuickDraw PictComment, marking the end of the QuickDraw proxy.
  59.         
  60.     The resulting PICT can be cut & pasted, or saved into a PICT file.  On a system
  61.     with QuickDraw GX™ installed, the GX data will be used when printing.  
  62.     When printing on other systems, the QuickDraw proxy will be used.
  63.     
  64.     The proxy is always used for display from within non-GX applications.
  65. */
  66. {
  67.     gxGraphicsError        gxErr = noErr;
  68.     PicHandle            thePicture;
  69.     Rect                picRect;
  70.     ShapeBeginRecord    theBegin;
  71.     Handle                hBegin;
  72.     ShapeEndHdl            hEnd;
  73.     gxRectangle            shapeBounds;
  74.         
  75.     // get the location of the shape
  76.     GXGetShapeDeviceBounds(theShape, 0, 0, &shapeBounds);    
  77.     picRect.left     = FixedToInt(shapeBounds.left);
  78.     picRect.top     = FixedToInt(shapeBounds.top);
  79.     picRect.right     = FixedToInt(shapeBounds.right);
  80.     picRect.bottom     = FixedToInt(shapeBounds.bottom);
  81.     GlobalToLocal((Point*) &picRect.top);
  82.     GlobalToLocal((Point*) &picRect.bottom);
  83.         
  84.     thePicture = OpenPicture(&picRect);
  85.     if (thePicture != nil)
  86.         {
  87.         // use a standard clipping and pen mode
  88.         ClipRect(&picRect);
  89.         PenNormal();
  90.         
  91.         // flatten our shape out into a handle
  92.         hBegin = ShapeToHandle(theShape);
  93.         if (hBegin != nil)
  94.             {
  95.             // add the comment to the begining of the handle
  96.             theBegin.signature     = shapeSignature;
  97.             theBegin.kind         = shapeBegin;
  98.             theBegin.bounds     = picRect;
  99.             Munger(hBegin, 0, nil, 0, &theBegin, sizeof(OSType) + sizeof(short) + sizeof(Rect) );
  100.             gxErr = MemError();
  101.         
  102.             // store the shape/handle into the picture
  103.             PicComment(shapeBegin, GetHandleSize(hBegin), hBegin);
  104.             DisposHandle(hBegin);
  105.             
  106.             // Send the dimensions of the shape at 72 dpi
  107.             PenMode(23);
  108.             FrameRect(&picRect);
  109.             PenNormal();
  110.             }
  111.         else
  112.             gxErr = MemError();
  113.     
  114.         if (gxErr == noErr)
  115.             {
  116.             if (simpleProxy)
  117.                 {
  118.                 // our proxy is just a framed rect with an X through it
  119.                 FrameRect(&picRect);
  120.                 MoveTo(picRect.left,     picRect.top);
  121.                 LineTo(picRect.right,     picRect.bottom);
  122.                 MoveTo(picRect.right,     picRect.top);
  123.                 LineTo(picRect.left,     picRect.bottom);
  124.                 }
  125.             else
  126.                 {
  127.                 // our proxy is a bitmap of the GX object
  128.                 gxBitmap        theBits;
  129.                 gxShape            theBitmap;
  130.  
  131.                 theBits.width         = picRect.right - picRect.left;
  132.                 theBits.height         = picRect.bottom - picRect.top;
  133.                 theBits.pixelSize     = 1;
  134.                 theBits.rowBytes    = ((theBits.width + 31) >> 5) << 2;
  135.                 theBits.image         = NewPtrClear(theBits.height * theBits.rowBytes);
  136.                 theBits.space         = gxIndexedSpace;
  137.                 theBits.set         = nil;
  138.                 theBits.profile     = nil;
  139.                 
  140.                 gxErr = MemError();
  141.                 if (gxErr == noErr)
  142.                     {
  143.                     theBitmap = GXNewBitmap(&theBits, nil);
  144.                     GXMoveTransformTo(GXGetShapeTransform(theBitmap), -shapeBounds.left, -shapeBounds.top);
  145.                     GXGetGraphicsError(&gxErr);
  146.                     if (gxErr == noErr)
  147.                         {
  148.                         BitMap        qdBits;
  149.                         
  150.                         // put the shape into the bitmap
  151.                         CopyToBitmaps(theBitmap, theShape);
  152.                         ConvertToQDBitmap(&theBits, &qdBits);
  153.                         GXGetGraphicsError(&gxErr);
  154.                                                                         
  155.                         // now done with the bitmap
  156.                         GXDisposeShape(theBitmap);
  157.  
  158.                         // CopyBits it into the picture
  159.                         CopyBits(&qdBits, &qd.thePort->portBits, &qdBits.bounds, &picRect, srcOr, nil);
  160.                         
  161.                         }
  162.                         
  163.                     // and done with the image
  164.                     DisposePtr(theBits.image);
  165.                     }
  166.                 }
  167.             
  168.             // mark the end of our shape's proxy
  169.             PicComment(shapeEnd, 0, (Handle) nil);
  170.             }
  171.         
  172.         ClosePicture();
  173.         }
  174.     else
  175.         gxErr = MemError();
  176.         
  177.     // if we had a problem, return a nil picture
  178.     if ( ( gxErr != noErr ) && thePicture )
  179.         {
  180.         KillPicture(thePicture);
  181.         thePicture = nil;
  182.         }
  183.         
  184.     return(thePicture);
  185.     
  186. } // PictureToPICT
  187.